package com.framework.seedkit.ui.uiframe.vpagerSlip;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.framework.seedkit.R;
import com.framework.seedkit.base.BaseFragmentActivity;
import com.framework.seedkit.ui.fragment.TestFrag1;
import com.framework.seedkit.ui.fragment.TestFrag2;
import com.framework.seedkit.ui.fragment.TestFrag3;
import com.framework.seedkit.ui.fragment.TestFrag4;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ViewPagerFlipActivity extends BaseFragmentActivity {
	private String TAG = "FragInFragAct";
	public List<Fragment> fragmentList;

	@BindView(R.id.pager)
	MainViewPager mainViewPager;
	@BindView(R.id.main_tab) RadioGroup mainTab;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
		sendReq();
	}

	private void init() {
		initView();
		addFragment();
	}

	/**
	 * 页面组件初始化
	 **/
	private void initView() {
		setListener();

		//默认首页被选中
		((RadioButton)(mainTab.getChildAt(0))).setChecked(true);
	}

	protected int getLayoutId() {
		return R.layout.act_viewpager_flip;
	}

	public void setTabSelected(int checkedId) {
		switch (checkedId) {
			case R.id.main_tab_home_btn:
				mainViewPager.setCurrentItem(0, false);
				break;
			case R.id.main_tab_bank_btn:
				mainViewPager.setCurrentItem(1, false);
				break;
			case R.id.main_tab_assist_btn:
				mainViewPager.setCurrentItem(2, false);
				break;
			case R.id.main_tab_user_btn:
				mainViewPager.setCurrentItem(3, false);
				break;
		}
	}

	private void addFragment() {
		fragmentList = new ArrayList<>();
		Fragment f1 = new TestFrag1();
		Fragment f2 = new TestFrag2();
		Fragment f3 = new TestFrag3();
		Fragment f4 = new TestFrag4();
		fragmentList.add(f1);
		fragmentList.add(f2);
		fragmentList.add(f3);
		fragmentList.add(f4);

		mainViewPager.setAdapter(new MainFrageAdapter(getSupportFragmentManager()));
	}

	/**
	 * 事件注册与回调处理
	 **/
	private void setListener() {
		//ViewPage手势滑动事件
		mainViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				Log.d(TAG, "onPageSelected>>>position:" + position);
				changeTabBtn(position);
			}

			@Override
			public void onPageScrolled(int curPosition, float progress, int deltaPx) {
				Log.d(TAG, "onPageScrolled>>>curPosition:" + curPosition + ">>>progress:" + progress + ">>>deltaPx:" + deltaPx);
			}

			@Override
			public void onPageScrollStateChanged(int position) {
				Log.d(TAG, "onPageScrollStateChanged>>>position:" + position);
			}

		});
		//RadioGroup点击切换事件
		mainTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				setTabSelected(checkedId);
			}
		});
	}

	/**
	 * 手动切换被选中的按钮
	 **/
	private  void changeTabBtn(int position) {
		((RadioButton)(mainTab.getChildAt(position))).setChecked(true);
	}

	/**
	 * 服务器请求数据
	 **/
	private void sendReq() {
	}

	/**
	 *  Fragment管理容器
	 **/
	private class MainFrageAdapter extends FragmentPagerAdapter {
		FragmentManager fm;

		public MainFrageAdapter(FragmentManager fm) {
			super(fm);
			this.fm = fm;
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = fragmentList.get(position % fragmentList.size());
			Log.i(TAG, "getItem:position=" + position + ",fragment:"
					+ fragment.getClass().getName() + ",fragment.tag="
					+ fragment.getTag());

			return fragment;
		}

		@Override
		public int getCount() {
			return fragmentList.size();
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			return super.instantiateItem(container, position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			super.destroyItem(container, position, object);
		}
	}
}